package webModule.product
{
	import com.kvc.extend.papervision3d.FloorPlane3D;
	import com.kvc.extend.papervision3d.FourCamerasView;
	import com.kvc.extend.papervision3d.PlaneGrid3D;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.papervision3d.core.proto.DisplayObjectContainer3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.layer.util.ViewportLayerSortMode;
	
	public class Product3DViewer extends FourCamerasView
	{
		static public const VIEW3D_ADDED:String="view3dAdded";
		static public const VIEW3D_REMOVED:String="view3dRemoved";
		
		private var light:PointLight3D;
		private var grid3d:PlaneGrid3D;
		private var container:DisplayObject3D;
		private var floorPlane:FloorPlane3D;
		private var product:Collada;
		private var closeButton:CloseButton;
		public function Product3DViewer(viewportWidth:Number=640, viewportHeight:Number=480, scaleToStage:Boolean=true, interactive:Boolean=false)
		{
			super(viewportWidth, viewportHeight, false, interactive);
			this.graphics.lineStyle(1,0x666666);
			this.graphics.beginFill(0x000000);
			this.graphics.drawRect(0,0,viewportWidth,viewportHeight);
			this.graphics.endFill();
			
			
			init3D();
			startRendering();
			
			closeButton=new CloseButton();
			closeButton.y=5;
			closeButton.x=viewportWidth-closeButton.width-5;
			addChild(closeButton);
			closeButton.addEventListener(MouseEvent.CLICK,remove3Dview);
			//addEventListener(Event.ADDED_TO_STAGE,addedToStage);
		}
		private function remove3Dview(evt:MouseEvent):void{
			this.dispatchEvent(new Event(Product3DViewer.VIEW3D_REMOVED));
		}
		private function addedToStage(evt:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE,addedToStage);
			
		}
		private function init3D() : void {
			light = new PointLight3D(false);
			light.z = -10000;
			light.y = 40000;
			light.x = 30000;
			scene.addChild(light);
			
			container=new DisplayObject3D();
			
			
			//grid3d=new PlaneGrid3D();
			//grid3d.y=-100;
			//container.addChild(grid3d);
			
			floorPlane=new FloorPlane3D();
			container.addChild(floorPlane);
			scene.addChild(container);
			viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;
			//addEventListener(Event.ENTER_FRAME, loop)
		}
		public function showProduct(pro:Collada):void{
			if(product) container.removeChild(product);
			container.addChild(pro);
			product=pro;
			
			//var pane:Plane=new Plane();
			//container.addChild(pane);
		}
	}
}

import com.touchX.display.AbstractDisplay;

import flash.events.Event;
import flash.events.MouseEvent;

class CloseButton extends AbstractDisplay{
	public function CloseButton():void{
		this.graphics.beginFill(0xff0000,0);
		this.graphics.drawRect(0,0,20,20);
		this.graphics.endFill();
		this.alpha=.3;
		this.buttonMode=true;
		
		this.graphics.lineStyle(2,0xffffff);
		this.graphics.moveTo(0,0);
		this.graphics.lineTo(20,20);
		this.graphics.moveTo(0,20);
		this.graphics.lineTo(20,0);
	}
	protected override function added(event:Event):void{
		super.added(event);
		addEvent(this,MouseEvent.MOUSE_OVER,changeState);
		addEvent(this,MouseEvent.MOUSE_OUT,changeState);
	}
	private function changeState(evt:MouseEvent):void{
		switch(evt.type){
			case MouseEvent.MOUSE_OVER:
				this.alpha=1;
				break;
			case MouseEvent.MOUSE_OUT:
				this.alpha=.3;
				break;
		}
	}
	protected override function removed(event:Event):void{
		super.removed(event);
		removeEvent(this,MouseEvent.MOUSE_OVER,changeState);
		removeEvent(this,MouseEvent.MOUSE_OUT,changeState);
	}
}